package com.czl.module_storehouse.mvp.presenter;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.BasePresenter;
import com.czl.module_base.mvp.view.IView;
import com.czl.module_base.observer.AbsHandleSubscriber;
import com.czl.module_storehouse.bean.AddStorageBean;
import com.czl.module_storehouse.bean.CardBean;
import com.czl.module_storehouse.bean.PurchaseBean;
import com.czl.module_storehouse.bean.StorageTypeModel;
import com.czl.module_storehouse.mvp.model.AddWareHouseModel;

import java.util.List;

/**
 * @author xubin
 */
public class AddWareHousePresenter extends BasePresenter<AddWareHouseModel, IView> {

    public static final String TAG_STORAGE_ADD = "add_warehouse_storage_add";
    public static final String TAG_STORE_PROJECT = "add_warehouse_storage_project";
    public static final String TAG_STORAGE_MODEL = "add_warehouse_storage_model";
    public static final String TAG_STORAGE_MATE_SORT_LIST = "add_warehouse_storage_mate_sort_list ";

    public void getSortAllList(int manageMode) {
        /*RequestBody body;
        if (0 == manageMode) {
            body = ParameterUtils.getRequestBodyByManageCompanyId();
        } else {
            body = ParameterUtils.getRequestBodyByManageManageMode(manageMode);
        }
        mModel.getSortAllList(body)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(Constants.MAX_RETRIES, Constants.RETRY_DELAY_SECOND))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) {
                        addDispose(disposable);
                        if (mRootView != null) mRootView.showDialog();
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (mRootView != null) mRootView.hideDialog();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<HttpResponse<ListBean<SortBean>>>(mRxErrorHandler, mRootView) {
                    @Override
                    public void onNext(HttpResponse<ListBean<SortBean>> httpResponse) {
                        if (null == mRootView) return;
                        mRootView.showHttpResponse(Constants.TAG_DATA_LIST, httpResponse);
                    }
                });*/
    }

    public void getSortList(Boolean pasteCode, int pageNo, List<Integer> list, int priceInfo, String sortName) {
        /*RequestBody body = ParameterUtils.getRequestBodyByManageManageModePage(pasteCode, pageNo, list, priceInfo, sortName);
        mModel.getSortList(body)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(Constants.MAX_RETRIES, Constants.RETRY_DELAY_SECOND))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) {
                        addDispose(disposable);
                        if (pageNo <= 1 && mRootView != null) mRootView.showLoading();
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
//                    if (mRootView != null) mRootView.hideDialog();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<HttpResponse<ListBean<SortBean>>>(mRxErrorHandler, mRootView, false, pageNo <= 1) {
                    @Override
                    public void onNext(HttpResponse<ListBean<SortBean>> httpResponse) {
                        super.onNext(httpResponse);
                        if (null == mRootView || !showSuccess) return;
                        mRootView.showHttpResponse(TAG_STORE_SORT_LIST, httpResponse);
                    }
                });*/
    }

    public void getStorageList(int pageNo) {
        /*RequestBody body = ParameterUtils.getRequestBodyByStorehouseIdPageNo(pageNo);
        mModel.getStorageList(body)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(Constants.MAX_RETRIES, Constants.RETRY_DELAY_SECOND))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) {
                        addDispose(disposable);
                        if (mRootView != null) mRootView.showLoading();
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<HttpResponse<ListBean<StorageRecordBean>>>(mRxErrorHandler, mRootView, false) {
                    @Override
                    public void onNext(HttpResponse<ListBean<StorageRecordBean>> httpResponse) {
                        super.onNext(httpResponse);
                        if (null == mRootView) return;
                        mRootView.showHttpResponse(TAG_STORE_RECORD_LIST, httpResponse);
                    }
                });*/
    }

    /**
     * 查询入库类型
     */
    public void getStorageModel() {
        doSubscribe2(mModel.getStorageModel(), new AbsHandleSubscriber<HttpResponse<List<StorageTypeModel>>>() {
            @Override
            public void onSuccess(HttpResponse<List<StorageTypeModel>> listHttpResponse) {
                listHttpResponse.setRequestTag(TAG_STORAGE_MODEL);
                mView.showHttpResponse(listHttpResponse);
            }
        }, false);
    }

    /**
     * 查询采购单
     */
    public void getCardList() {
        doSubscribe2(mModel.getCardList(), new AbsHandleSubscriber<HttpResponse<List<CardBean>>>() {
            @Override
            public void onSuccess(HttpResponse<List<CardBean>> listHttpResponse) {
                mView.showHttpResponse(listHttpResponse);
            }
        });
    }

    /**
     * 入库
     *
     * @param bean
     */
    public void addStorage(AddStorageBean bean) {
        doSubscribe2(mModel.addStorage(bean), new AbsHandleSubscriber<HttpResponse<Object>>() {
            @Override
            public void onSuccess(HttpResponse<Object> httpResponse) {
                httpResponse.setRequestTag(TAG_STORAGE_ADD);
                mView.showHttpResponse(httpResponse);
            }
        });
    }

    public void selectCrmCompanyByTag() {
       /* RequestBody body = ParameterUtils.getRequestBodyByTag(Constants.TAG_SUPPLIER);
        mModel.selectCrmCompanyByTag(body)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay())
                .doOnSubscribe(disposable -> {
                    addDispose(disposable);
                    if (null != mRootView) mRootView.showDialog();
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (null != mRootView) mRootView.hideDialog();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<HttpResponse<ListBean<CompanyBean>>>(mRxErrorHandler, mRootView) {
                    @Override
                    public void onNext(HttpResponse<ListBean<CompanyBean>> httpResponse) {
                        super.onNext(httpResponse);
                        if (null == mRootView || !showSuccess) return;
                        mRootView.showHttpResponse(TAG_STORE_COMPANY, httpResponse);
                    }
                });*/
    }

    public void appgetRukuPurhchaseCardList() {
        doSubscribe(mModel.appgetRukuPurhchaseCardList(), new AbsHandleSubscriber<HttpResponse<ListBean<PurchaseBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<PurchaseBean>> listHttpResponse) {
                mView.showData(listHttpResponse.getData());
            }
        });
    }

    public void selectProjectInStoreHouse() {
       /* int storehouseId = ParameterUtils.getStorehouseId();
        int manageCompanyId = ParameterUtils.getManageCompanyId();
        RequestBody body = ParameterUtils.getRequestBodyByStorehouseId(storehouseId, manageCompanyId);
        mModel.selectProjectInStoreHouse(body)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay())
                .doOnSubscribe(disposable -> {
                    addDispose(disposable);
                    if (null != mRootView) mRootView.showLoading();
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
//                    if (null != mRootView) mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<HttpResponse<ListBean<StoreProjectBean>>>(mRxErrorHandler, mRootView, false) {
                    @Override
                    public void onNext(HttpResponse<ListBean<StoreProjectBean>> httpResponse) {
                        super.onNext(httpResponse);
                        if (null == mRootView || !showSuccess) return;
                        mRootView.showHttpResponse(TAG_STORE_PROJECT, httpResponse);
                    }
                });*/
    }

    public void mateSortList(List<SortBean> list) {
        doSubscribe2(mModel.mateSortList(list), new AbsHandleSubscriber<HttpResponse<ListBean<SortBean>>>() {
            @Override
            public void onSuccess(HttpResponse<ListBean<SortBean>> httpResponse) {
                httpResponse.setRequestTag(TAG_STORAGE_MATE_SORT_LIST);
                mView.showHttpResponse(httpResponse);
            }
        });
    }
}
